/**
 * ЯegExpR User Interface - regexpr.ui
 * Copyright 2010
 * Released under the MIT and GPL licenses.
 * 
 * Author      : y@s
 * Version     : 0.0.5
 * Published   : 2010-05-07
 * Last Update : 2010-05-25
 */
/*@cc_on
var doc = document;
eval('var document = doc');
@*/
;(function( $, undefined ){
var
GTable = {
	create: function( element, options ){
		
		var
		opt = $.extend({
			th      : ["name","value"],
			visible : true
		},options),
		fn = function (){
			this.initialize.apply(this, arguments);
		},
		table, data, view;
		
		fn.prototype = {
			$content: null,
			initialize: function( elm ){
				table = new google.visualization.Table( elm );
				this.draw();
				this.$content = $(elm)[opt.visible === true ? "show" : "hide"]();
			},
			//set data
			set: function ( v ){
				var i = 0, rows = [], j;
				this.clear();
				
				//make rows
				if (v) {
					j = v.length;
					for ( ; i < j ; i++ ) {
						rows.push({c:[{v: v[i].key}, {v: v[i].value}]});
					}
				}
				
				this.draw(rows);
			},
			//remove rows
			clear: function(){
				var n = data.getNumberOfRows();
				if ( n ) {
					data.removeRows( 0, n );
				}
				return this;
			},
			// draw data rows
			draw:function(rows){
				data = new google.visualization.DataTable({
					cols: [
						{label: opt.th[0], type : 'string'}, 
						{label: opt.th[1], type : 'string'}
					],
					rows: rows || []
				});
				view = new google.visualization.DataView(data);
				table.draw(view, {allowHtml: true, showRowNumber: true});
				return this;
			},
			// clear table
			reset: function(){
				return data.getNumberOfRows() ? this.clear().draw() : this;
			},
			// hide table element
			hide: function( callback ){
				this.$content
					.find(":first").css("position","")
					.find(":first").css("position","");
				this.$content.slideUp(400, callback);
				return this;
			},
			// show table element
			show: function( callback ){
				this.$content
					.find(":first").css("position","")
					.find(":first").css("position","");
				this.$content.slideDown(400, callback);
				return this;
			},
			// show or hide toggle
			toggle: function( callback ){
				this[this.$content.is(":hidden") ? "show" : "hide"](callback || function(){});
			}
		};
		
		return new fn( element );
	}
},

//default settings
patternDefaultValue = "/\\d+/g",
replaceDefaultValue = "{{replace}}",
targetDefaultValue  = "abc\\123 \"d'e'f\",45_6-789\n\t(ghi)!#$%&'\",.?+*\n[xyz]<p>{012}<p>>JavaScript'ABC'></p>^~\/<</p>>";

//RegExp(r) User Interface.
$.regexpr["ui"] = function(){
	
	var
	$orderButtons   = $("#exec-order span.exec-order-button"),
	$patternTextBox = $("#inputbox-pattern"),
	$replaceTextBox = $("#parambox-replace"),
	$targetTextarea = $("#parambox-target"),
	$resultTextarea = $("#parambox-result"),
	logToggleBox    = $.toggleBox.create($("#logbox div.togglebox"), {text:"Log:"}),
	prfToggleBox    = $.toggleBox.create($("#profilebox div.togglebox"), {text:"Profile:"}),
	logGtable       = GTable.create(document.getElementById("logbox-table"), {th:["property name","value"]}),
	prfGtable       = GTable.create(document.getElementById("profilebox-table"), {th:["keyword","value"]}),
	
	logOn = function (){
		$.regexpr.log(function(log){
			logGtable.set(log);
		});
	},
	logOff = function (){
		logGtable.reset();
	},
	profileOn = function (){
		$.regexpr.profile(function(profile){
			prfGtable.set(profile);
		});
	},
	profileOff = function (){
		prfGtable.reset();
	};
	
	//set default value
	$patternTextBox.val(patternDefaultValue);
	$replaceTextBox.val(replaceDefaultValue);
	$targetTextarea.val(targetDefaultValue);
	
	$patternTextBox
		.add($replaceTextBox)
		.add($targetTextarea)
		.add($resultTextarea)
		.focus(function(){
			$(this).parent().addClass("focus");
		})
		.blur(function(){
			$(this).parent().removeClass("focus");
		});
	
	$orderButtons
		.parent()
		.mouseup(function(){
			$(this).removeClass("mousedown");
		})
		.mouseout(function(){
			$(this).removeClass("mousedown");
		})
		.mousedown(function(){
			$(this).addClass("mousedown");
		});

	$orderButtons
		.bind( "click", function (){
			$resultTextarea.val($.regexpr.check({
				pattern: $.trim($patternTextBox.val() || ""),
				target : $targetTextarea.val() || "",
				order  : $(this).text() || "match",
				replace: $replaceTextBox.val() || ""
			}));
		})
		.bind( "click", logOn     )
		.bind( "click", profileOn );
	
	logToggleBox.bindStateChanged(function(e, state){
		logGtable.toggle();
		$orderButtons
			.unbind( "click", state ? logOff : logOn  )
			.bind(   "click", state ? logOn  : logOff );
	});
	
	prfToggleBox.bindStateChanged(function(e, state){
		prfGtable.toggle();
		$orderButtons
			.unbind("click", state ? profileOff : profileOn  )
			.bind(  "click", state ? profileOn  : profileOff );
	});
};
})(jQuery);
jQuery(function($){ $.regexpr.ui(); });